<?php
/**
 * This makes our life easier when dealing with paths. Everything is relative
 * to the application root now.
 */


use Zend\Db\Adapter\Adapter;
use Zend\Db\TableGateway\TableGateway;
use Zend\Db\ResultSet\HydratingResultSet;

define('ROOT_PATH', dirname(__DIR__));

chdir(dirname(__DIR__));

define('REQUEST_MICROTIME', microtime(true));

// Setup autoloading
require 'init_autoloader.php';

// Run the application!
//Zend\Mvc\Application::init(require 'config/application.config.php')->run();


$configuration = require 'config/application.config.php';


$adapter = new Zend\Db\Adapter\Adapter(array(
		'driver'   => 'pdo_mysql',
		'database' => 'zf2-dev',
		'username' => 'root',
		'password' => 'tham1411'
));


$modulesTable = new TableGateway('modules', $adapter, null, new HydratingResultSet());

$rowset = $modulesTable->select(array('state' => 0))->toArray();

$modules = array();

$count = count($configuration['modules']);

foreach($rowset as $row)
{
	$modules[$count++] = $row['key_name'];
}

$configuration['modules'] = $configuration['modules'] + $modules;

$smConfig = isset($configuration['service_manager']) ? $configuration['service_manager'] : array();

$serviceManager = new \Zend\ServiceManager\ServiceManager(new \Zend\Mvc\Service\ServiceManagerConfig($smConfig));

$serviceManager->setService('ApplicationConfig', $configuration);

$serviceManager->get('ModuleManager')->loadModules();

//run the app
$serviceManager->get('Application')->bootstrap()->run();